package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.constant.UserConstants;
import com.tbit.uqbike.object.pojo.LeaderAccount;
import com.tbit.uqbike.service.business.LeaderAccountService;
import com.tbit.uqbike.webmanager.dao.core.LeaderAccountDao;
import com.tbit.uqbike.webmanager.util.DateUtils;
import com.tbit.uqbike.webmanager.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 领导账号Service业务层处理
 *
 * @author jing
 * @date 2021-05-25
 */
@Service
public class LeaderAccountServiceImpl implements LeaderAccountService {
    @Autowired
    private LeaderAccountDao leaderAccountDao;


    /**
     * 查询领导账号列表
     *
     * @param leaderAccount 领导账号
     * @return 领导账号
     */
    @Override
    public List<LeaderAccount> selectLeaderAccountList(LeaderAccount leaderAccount) {
        return leaderAccountDao.selectLeaderAccountList(leaderAccount);
    }

    @Override
    public void checkExpireUser() {
        List<String> phones = leaderAccountDao.selectLeaderAccountList(new LeaderAccount(DateUtils.getNowDate(), 1)).stream().map(LeaderAccount::getPhone).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(phones)) {
            leaderAccountDao.updateEnableByPhones(phones);
        }
    }

    @Override
    public String checkPhoneUnique(LeaderAccount leaderAccount) {
        List<LeaderAccount> leaderAccounts = leaderAccountDao.selectLeaderAccountList(new LeaderAccount(leaderAccount.getAccountId(), leaderAccount.getPhone()));
        if (leaderAccounts.size() > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增领导账号
     *
     * @param leaderAccount 领导账号
     * @return 结果
     */
    @Override
    public int insertLeaderAccount(LeaderAccount leaderAccount) {
        leaderAccount.setAddTime(DateUtils.getNowDate());
        return leaderAccountDao.insertLeaderAccount(leaderAccount);
    }

    /**
     * 修改领导账号
     *
     * @param leaderAccount 领导账号
     * @return 结果
     */
    @Override
    public int updateLeaderAccount(LeaderAccount leaderAccount) {
        return leaderAccountDao.updateLeaderAccount(leaderAccount);
    }

    /**
     * 批量删除领导账号
     *
     * @param accountIds 需要删除的领导账号ID
     * @return 结果
     */
    @Override
    public int deleteLeaderAccountByIds(LeaderAccount leaderAccount) {
        return leaderAccountDao.deleteLeaderAccountByIds(leaderAccount);
    }
}
